/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ita.escolaita.view.converter;

import br.ita.escolaita.model.entity.Aluno;
import java.util.List;
import javax.faces.component.UIComponent;
import javax.faces.component.UISelectItems;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.model.SelectItem;

/**
 *
 * @author Rafael
 */
public class AlunoConverter implements Converter {

    public Object getAsObject(FacesContext ctx, UIComponent comp, String value) {
        UISelectItems selectItems = (UISelectItems) comp.getChildren().get(1);
        List listaSelectItem = (List) selectItems.getValue();
        for (Object o : listaSelectItem) {
            SelectItem item = (SelectItem) o;
            Aluno a = (Aluno) item.getValue();
            if (a.getId().equals(Integer.valueOf(value))) {
                return a;
            }
        }
        return null;
    }

    public String getAsString(FacesContext ctx, UIComponent comp, Object obj) {
        if (obj == null) {
            return null;
        }
        if (obj instanceof String){
            return "";
        }
        Aluno a = (Aluno) obj;
        return a.getId().toString();
    }
}
